﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Webdiyer.WebControls.Mvc;
using ElingOA6_DSJCJ.Common;

namespace ElingOA6_DSJCJ.Controllers
{
    public class HonorsEditionController : Controller
    {
        private BLL.Album albumBLL = new BLL.Album();
        private BLL.Common commonBll = new BLL.Common();
        BLL.AlbumCls aclsbll = new BLL.AlbumCls();
        private BLL.AlbumEntry albumEntryBLL = new BLL.AlbumEntry();

        //
        // GET: /RYB/

        public ActionResult Index(string FAlbumClsNo)
        {
        List<Model.AlbumCls> aclslist=   aclsbll.GetModelList("");
        string docClsHtml = "";
        docClsHtml += "<ul>";
        foreach (Model.AlbumCls dr in aclslist)
        {
            docClsHtml += "<li><h2><a href='/HonorsEdition/AlbumList?albumClsNo=" + dr.FAlbumClsNo + "'  target='contentFrm' >" + dr.FAlbumClsName.ToString() + "</a></h2></li>";

            //docClsHtml += "<li><a href='/Index/DocList?keyName=" + keyName +
            //  "&keyClass=" + keyClass + "&docClsCd=" + dr["FDocClsCd"].ToString()
            //  + "' target='contentFrm' >" + dr["FDocClsName"].ToString() + "</a></li>";
          
        }
            if (string.IsNullOrEmpty(FAlbumClsNo))
            {
                if (aclslist.Count > 0)
                {

                    FAlbumClsNo = aclslist[0].FAlbumClsNo.ToString();

                }
            }
        docClsHtml += "</ul>";
        ViewData["docClsHtml"] = docClsHtml;
        ViewData["FAlbumClsNo"] = FAlbumClsNo;
            return View();
        }
        public ActionResult AlbumList(string albumClsNo, string clsCd, int id = 1)
        {
            Model.AlbumCls aclsmodel = aclsbll.GetModel(albumClsNo);
            ViewData["docClsCd"] = clsCd;
            //活动掠影
            string fiedls = "*";
            int total = 0;
            int pageRowSize = 15;
            int pageIndex = id;
            string ablumClsNo_hdly = albumClsNo;
            string where = " FAlbumClsNo='" + ablumClsNo_hdly + "'";
            DataSet ds = commonBll.GetListByPage("t_Album", fiedls, pageRowSize, pageIndex, where, "FAlbumId", 0, out total);
            List<Model.Album> albumList = albumBLL.DataTableToList(ds.Tables[0]).OrderBy(h=>h.FDispSeq).ToList();
            ViewData["albumList"] = albumList;
            ViewData["aclsname"] = aclsmodel.FAlbumClsName;
            ViewData["albumClsNo"] = albumClsNo;
            PagedList<Model.Album> documentList = new PagedList<Model.Album>(albumList, id, pageRowSize, total);
            return View(documentList);
           
        }

        /// <summary>
        /// 图片播放页面
        /// </summary>
        /// <param name="docID"></param>
        /// <param name="entryID"></param>
        /// <returns></returns>
        public ActionResult PicShow(int albumId, int entryID)
        {
            List<Model.AlbumEntry> albumEntryList = albumEntryBLL.GetListByAlbumId(albumId);
            string firstImg = string.Empty;
            string photoHtml = string.Empty;
            if (albumEntryList.Count > 0)
            {
                if (albumEntryList[0].FAlbumEntryID != entryID && entryID != -1)//如果点击的图片不是取得列表的第一张，则设置隐藏域值，方便前台调用图片从选择图片开始播放,-1表示是从目录生成中跳转过来的，不判断从第几张开始播放
                {
                    firstImg = "img" + entryID;
                }
                System.Text.StringBuilder sbr = new System.Text.StringBuilder();
                System.Text.StringBuilder sbrUl = new System.Text.StringBuilder();

                sbr.Append("<div id=\"photos\" class=\"galleryview\">");
                sbrUl.Append("<ul class=\"filmstrip\">");

                foreach (Model.AlbumEntry acc in albumEntryList)
                {
                    sbr.Append("<div class=\"panel\">");
                    sbr.Append("<img src=\"");
                    sbr.Append(Url.Content(acc.FAlbumPhotoURL));
                    sbr.Append("\" width=\"800px\" height=\"490px\" />");
                    sbr.Append("<div class=\"panel-overlay\" style=\"text-align:center;\" >");
                    sbr.Append("<p><font  style=\"font-size:16px\">");
                    sbr.Append(acc.FAlbumNote);
                    sbr.Append("</font></p></div></div>");

                    sbrUl.Append("<li><img src=\"");
                    sbrUl.Append(Url.Content(acc.FAlbumThumbURL));
                    sbrUl.Append("\" id=\"img");
                    sbrUl.Append(acc.FAlbumEntryID);
                    sbrUl.Append("\" width=\"100px\" height=\"60px\" /> </li>");
                }

                sbrUl.Append("</ul>");
                sbr.Append(sbrUl.ToString());
                sbr.Append("</div>");

                photoHtml = sbr.ToString();
            }


            //增加背景音乐和图片时间间隔
            string bgMusicUrl = string.Empty;
            int playInterval = 6000;

            List<string> _list = albumBLL.GetBGMusicPathAndPlayInterval(albumId);
            if (_list.Count == 2)
            {
                if (_list[0].Trim() != "")
                {
                    bgMusicUrl = _list[0].Trim();
                }
                if (_list[1].Trim() != "")
                {
                    int.TryParse(_list[1].Trim(), out playInterval);
                    playInterval = playInterval * 1000;
                }
            }



            ViewData["firstImg"] = firstImg;
            ViewData["photoHtml"] = photoHtml;

            ViewData["bgMusicUrl"] = bgMusicUrl;
            ViewData["playInterval"] = playInterval;


            return View("PicShow");
        }

        /// <summary>
        /// 相册图片压缩
        /// </summary>
        /// <returns></returns>
        public void AlbumIndexImage(int albumId)
        {
            Model.Album albumModel = albumBLL.GetModel(albumId);
            if (albumModel != null)
            {
                if (GenerateUtil.ExistsFile(albumModel.FFilePath))//图片存在
                {
                    int width = 298;
                    int height = 223;
                    byte[] imgs = OperateImg.breviaryImg(width, height, albumModel.FFilePath, 0);

                    Response.AppendHeader("Content-Length", imgs.Length.ToString());

                    //Response.Clear();
                    Response.ContentType = "image/jpeg";
                    Response.BinaryWrite(imgs);
                    Response.End();
                }
            }
        }

        /// <summary>
        /// 相册图片压缩
        /// </summary>
        /// <returns></returns>
        public void AlbumImage(int albumId)
        {
            Model.Album albumModel = albumBLL.GetModel(albumId);
            if (albumModel != null)
            {
                byte[] imgs = albumModel.FContentBin;

                Response.AppendHeader("Content-Length", imgs.Length.ToString());

                //Response.Clear();
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite(imgs);
                Response.End();
            }


        }
    }
}
